#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    //Initialize classes
    m_textColorTab = new textColorTab;
    m_loadingColorTab = new loadingColorTab;
    m_dialogBoxesTab = new dialogBoxesTab;
    m_themeInfoTab = new themeInfoTab;

    //Connect elements for Menu Bar
    QObject::connect(ui->actionOpen, SIGNAL(triggered()), this, SLOT(openFile()));
    QObject::connect(ui->actionSave, SIGNAL(triggered()), this, SLOT(saveFile()));
    QObject::connect(ui->actionAbout_Qt, SIGNAL(triggered()), this, SLOT(aboutQt()));
    QObject::connect(ui->actionClose, SIGNAL(triggered()), this, SLOT(close()));
    QObject::connect(ui->actionAbout, SIGNAL(triggered()), this, SLOT(aboutMyself()));

    //Connect elements for Tab#1 (Color text)
    QObject::connect(this, SIGNAL(jsonDataUpdated(QString)), m_textColorTab, SLOT(updateLabels(QString)));
    QObject::connect(m_textColorTab, SIGNAL(setLabelsColor(QColor,QColor,QColor,QColor,QColor)), this, SLOT(updateLabelColor(QColor,QColor,QColor,QColor,QColor)));

    QObject::connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(updatePrimaryColor()));
    QObject::connect(ui->pushButton_2, SIGNAL(clicked()), this, SLOT(updateSecondaryColor()));
    QObject::connect(ui->pushButton_3, SIGNAL(clicked()), this, SLOT(updateIconSetColor()));
    QObject::connect(ui->pushButton_4, SIGNAL(clicked()), this, SLOT(updateIconSetActiveColor()));
    QObject::connect(ui->pushButton_5, SIGNAL(clicked()), this, SLOT(updateTransitionScreenColor()));

    //Connect elements for Tab#2 (Loading text)
    QObject::connect(this, SIGNAL(jsonDataUpdated(QString)), m_loadingColorTab, SLOT(updateLabels(QString)));
    QObject::connect(m_loadingColorTab, SIGNAL(setLabelsColor(QColor,QColor,QColor,QColor,QColor)), this, SLOT(updateLabelLoadingColor(QColor,QColor,QColor,QColor,QColor)));

    QObject::connect(ui->pushButton_6, SIGNAL(clicked()), this, SLOT(updateLoadingIconColor()));
    QObject::connect(ui->pushButton_7, SIGNAL(clicked()), this, SLOT(updateLoadingTextColor()));
    QObject::connect(ui->pushButton_8, SIGNAL(clicked()), this, SLOT(updateFreTextColor()));
    QObject::connect(ui->pushButton_9, SIGNAL(clicked()), this, SLOT(updateVersionTextColor()));
    QObject::connect(ui->pushButton_10, SIGNAL(clicked()), this, SLOT(updatePercentageTextColor()));

    //Connect elements for Tab#3 (Dialog and Boxes)
    QObject::connect(this, SIGNAL(jsonDataUpdated(QString)), m_dialogBoxesTab, SLOT(updateLabels(QString)));
    QObject::connect(m_dialogBoxesTab, SIGNAL(setLabelsColor(QColor,QColor,QColor,QColor,QColor)), this, SLOT(updateLabelDialogBoxesColor(QColor,QColor,QColor,QColor,QColor)));

    QObject::connect(ui->pushButton_11, SIGNAL(clicked()), this, SLOT(updateBoxColor()));
    QObject::connect(ui->pushButton_12, SIGNAL(clicked()), this, SLOT(updateBoxOutlineColor()));
    QObject::connect(ui->pushButton_13, SIGNAL(clicked()), this, SLOT(updateDialogBackground()));
    QObject::connect(ui->pushButton_14, SIGNAL(clicked()), this, SLOT(updateDialogButton()));
    QObject::connect(ui->pushButton_15, SIGNAL(clicked()), this, SLOT(updateDialogButtonText()));

    //Connect elements for Tab#4 (Theme informations)
    QObject::connect(this, SIGNAL(jsonDataUpdated(QString)), m_themeInfoTab, SLOT(updateData(QString)));
    QObject::connect(m_themeInfoTab, SIGNAL(setTextBox(QString,QString,QString)), this, SLOT(updateThemeInfos(QString,QString,QString)));

    QObject::connect(ui->lineEdit, SIGNAL(editingFinished()), this, SLOT(updateThemeName()));
    QObject::connect(ui->lineEdit_2, SIGNAL(editingFinished()), this, SLOT(updateThemeVersion()));
    QObject::connect(ui->textEdit, SIGNAL(textChanged()), this, SLOT(updateThemeDescription()));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::openFile()
{
    //Open the file dialog, showing only json files
    QString fileName = QFileDialog::getOpenFileName(this, tr("Open Config File"),
                                                    QDir::homePath(),
                                                    tr("JSON Files (*.json)"));

    //Open the file in a text stream
    QFile configFile(fileName);
    if (!configFile.open(QFile::ReadOnly | QFile::Text)) return;
    QTextStream in(&configFile);
    m_jsonData = in.readAll();

    //Convert json content to Json Object
    QJsonDocument jsonDataDocument = QJsonDocument::fromJson(m_jsonData.toUtf8());
    m_jsonObject = jsonDataDocument.object();

    //Emit the update event
    qDebug() << m_jsonObject;
    emit jsonDataUpdated(m_jsonData);
}

void MainWindow::saveFile()
{
    //Open the save file dialog, showing only json files
    QString fileName = QFileDialog::getSaveFileName(this, tr("Save Config File"),
                                                    QDir::homePath(),
                                                    tr("JSON Files (*.json)"));

    //Remove the file to clear it's content
    QFile::remove(fileName);
    //Open the file in a text stream
    QFile configFile(fileName);
    if (!configFile.open(QFile::ReadWrite | QFile::Text)) return;
    QTextStream in(&configFile);

    //Write the QJsonObject to the file
    QJsonDocument doc(m_jsonObject);
    in << doc.toJson(QJsonDocument::Compact);
}

void MainWindow::updateLabelColor(QColor labelPrimaryColor, QColor labelSecondaryColor, QColor labelIconSetColor, QColor labelSetActiveColor, QColor labelTransitionScreen)
{
    //Update the preview labels color for Tab#1
    ui->label->setStyleSheet("QLabel {color : " + labelPrimaryColor.name() + "}");
    ui->label_2->setStyleSheet("QLabel {color : " + labelSecondaryColor.name() + "}");
    ui->label_3->setStyleSheet("QLabel {color : " + labelIconSetColor.name() + "}");
    ui->label_4->setStyleSheet("QLabel {color : " + labelSetActiveColor.name() + "}");
    ui->label_5->setStyleSheet("QLabel {color : " + labelTransitionScreen.name() + "}");
}

void MainWindow::updatePrimaryColor()
{
    //Get color from user
    QString colorName = m_textColorTab->getColorFromUser().name();

    //Update Primary Text Label color + write new value to the JSON Object
    ui->label->setStyleSheet("QLabel {color : " + colorName + "}");
    m_jsonObject["primaryText"] = QJsonValue(QString(colorName.mid(1, 6)));
}

void MainWindow::updateSecondaryColor()
{
    //Get color from user
    QString colorName = m_textColorTab->getColorFromUser().name();

    //Update Secondary Text Label color + write new value to the JSON Object
    ui->label_2->setStyleSheet("QLabel {color : " + colorName + "}");
    m_jsonObject["secondaryText"] = QJsonValue(QString(colorName.mid(1, 6)));
}

void MainWindow::updateIconSetColor()
{
    //Get color from user
    QString colorName = m_textColorTab->getColorFromUser().name();

    //Update IconSet Text Label color + write new value to the JSON Object
    ui->label_3->setStyleSheet("QLabel {color : " + colorName + "}");
    m_jsonObject["iconSet"] = QJsonValue(QString(colorName.mid(1, 6)));
}

void MainWindow::updateIconSetActiveColor()
{
    //Get color from user
    QString colorName = m_textColorTab->getColorFromUser().name();

    //Update IconSet Text Label color + write new value to the JSON Object
    ui->label_4->setStyleSheet("QLabel {color : " + colorName + "}");
    m_jsonObject["iconSetActive"] = QJsonValue(QString(colorName.mid(1, 6)));
}

void MainWindow::updateTransitionScreenColor()
{
    //Get color from user
    QString colorName = m_textColorTab->getColorFromUser().name();

    //Update IconSet Text Label color + write new value to the JSON Object
    ui->label_5->setStyleSheet("QLabel {color : " + colorName + "}");
    m_jsonObject["transitionScreen"] = QJsonValue(QString(colorName.mid(1, 6)));
}

void MainWindow::aboutQt()
{
    //Just to open the "About Qt" dialog
    QMessageBox::aboutQt(this);
}

void MainWindow::aboutMyself()
{
    //Open the "About Timmy Companion" dialog
    QMessageBox::about(this, "About Timmy Companion", "Timmy Companion\n\nMake freeShop themer's life easier (Sometimes)\nv. " APP_VERSION);
}

void MainWindow::updateThemeInfos(QString themeName, QString themeVer, QString themeDesc)
{
    ui->lineEdit->setText(themeName);
    ui->lineEdit_2->setText(themeVer);
    ui->textEdit->setText(themeDesc);
}

void MainWindow::updateThemeName()
{
    m_jsonObject["themeName"] = QJsonValue(ui->lineEdit->text());
}

void MainWindow::updateThemeVersion()
{
    m_jsonObject["themeVer"] = QJsonValue(ui->lineEdit_2->text());
}

void MainWindow::updateThemeDescription()
{
    m_jsonObject["themeDesc"] = QJsonValue(ui->textEdit->toPlainText());
}

void MainWindow::updateLabelLoadingColor(QColor labelLoadingIconColor, QColor labelLoadingTextColor, QColor labelFreTextColor, QColor labelVersionTextColor, QColor labelPercentageTextColor)
{
    //Update the preview labels color for Tab#2
    ui->label_9->setStyleSheet("QLabel {color : " + labelLoadingIconColor.name() + "}");
    ui->label_10->setStyleSheet("QLabel {color : " + labelLoadingTextColor.name() + "}");
    ui->label_11->setStyleSheet("QLabel {color : " + labelFreTextColor.name() + "}");
    ui->label_12->setStyleSheet("QLabel {color : " + labelVersionTextColor.name() + "}");
    ui->label_13->setStyleSheet("QLabel {color : " + labelPercentageTextColor.name() + "}");
}

void MainWindow::updateLoadingIconColor()
{
    //Get color from user
    QString colorName = m_loadingColorTab->getColorFromUser().name();

    //Update Loading Icon Label color + write new value to the JSON Object
    ui->label_9->setStyleSheet("QLabel {color : " + colorName + "}");
    m_jsonObject["loadingColor"] = QJsonValue(QString(colorName.mid(1, 6)));
}

void MainWindow::updateLoadingTextColor()
{
    //Get color from user
    QString colorName = m_loadingColorTab->getColorFromUser().name();

    //Update Loading Text Label color + write new value to the JSON Object
    ui->label_10->setStyleSheet("QLabel {color : " + colorName + "}");
    m_jsonObject["loadingText"] = QJsonValue(QString(colorName.mid(1, 6)));
}

void MainWindow::updateFreTextColor()
{
    //Get color from user
    QString colorName = m_loadingColorTab->getColorFromUser().name();

    //Update Fre Text Label color + write new value to the JSON Object
    ui->label_11->setStyleSheet("QLabel {color : " + colorName + "}");
    m_jsonObject["freText"] = QJsonValue(QString(colorName.mid(1, 6)));
}

void MainWindow::updateVersionTextColor()
{
    //Get color from user
    QString colorName = m_loadingColorTab->getColorFromUser().name();

    //Update freeShop Version Text Label color + write new value to the JSON Object
    ui->label_12->setStyleSheet("QLabel {color : " + colorName + "}");
    m_jsonObject["versionText"] = QJsonValue(QString(colorName.mid(1, 6)));
}

void MainWindow::updatePercentageTextColor()
{
    //Get color from user
    QString colorName = m_loadingColorTab->getColorFromUser().name();

    //Update Percentage Text Label color + write new value to the JSON Object
    ui->label_13->setStyleSheet("QLabel {color : " + colorName + "}");
    m_jsonObject["percentageText"] = QJsonValue(QString(colorName.mid(1, 6)));
}

void MainWindow::updateLabelDialogBoxesColor(QColor labelBoxColor, QColor labelBoxOutlineColor, QColor labelDialogBackground, QColor labelDialogButton, QColor labelDialogButtonText)
{
    //Update the preview labels color for Tab#3
    ui->label_14->setStyleSheet("QLabel {color : " + labelBoxColor.name() + "}");
    ui->label_15->setStyleSheet("QLabel {color : " + labelBoxOutlineColor.name() + "}");
    ui->label_16->setStyleSheet("QLabel {color : " + labelDialogBackground.name() + "}");
    ui->label_17->setStyleSheet("QLabel {color : " + labelDialogButton.name() + "}");
    ui->label_18->setStyleSheet("QLabel {color : " + labelDialogButtonText.name() + "}");
}

void MainWindow::updateBoxColor()
{
    //Get color from user
    QString colorName = m_loadingColorTab->getColorFromUser().name();

    //Update Loading Icon Label color + write new value to the JSON Object
    ui->label_14->setStyleSheet("QLabel {color : " + colorName + "}");
    m_jsonObject["boxColor"] = QJsonValue(QString(colorName.mid(1, 6)));
}

void MainWindow::updateBoxOutlineColor()
{
    //Get color from user
    QString colorName = m_loadingColorTab->getColorFromUser().name();

    //Update Loading Text Label color + write new value to the JSON Object
    ui->label_15->setStyleSheet("QLabel {color : " + colorName + "}");
    m_jsonObject["boxOutlineColor"] = QJsonValue(QString(colorName.mid(1, 6)));
}

void MainWindow::updateDialogBackground()
{
    //Get color from user
    QString colorName = m_loadingColorTab->getColorFromUser().name();

    //Update Fre Text Label color + write new value to the JSON Object
    ui->label_16->setStyleSheet("QLabel {color : " + colorName + "}");
    m_jsonObject["dialogBackground"] = QJsonValue(QString(colorName.mid(1, 6)));
}

void MainWindow::updateDialogButton()
{
    //Get color from user
    QString colorName = m_loadingColorTab->getColorFromUser().name();

    //Update freeShop Version Text Label color + write new value to the JSON Object
    ui->label_17->setStyleSheet("QLabel {color : " + colorName + "}");
    m_jsonObject["dialogButton"] = QJsonValue(QString(colorName.mid(1, 6)));
}

void MainWindow::updateDialogButtonText()
{
    //Get color from user
    QString colorName = m_loadingColorTab->getColorFromUser().name();

    //Update Percentage Text Label color + write new value to the JSON Object
    ui->label_18->setStyleSheet("QLabel {color : " + colorName + "}");
    m_jsonObject["dialogButtonText"] = QJsonValue(QString(colorName.mid(1, 6)));
}
